package com.leetcode.根据数据结构分类.二叉树;

import com.leetcode.datastructure.TreeNode;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: ZhouBert
 * @date: 2021/4/13
 * @description: 783. 二叉搜索树节点最小距离
 * https://leetcode-cn.com/problems/minimum-distance-between-bst-nodes/
 */
public class A_783_二叉搜索树节点最小距离 {
	static A_783_二叉搜索树节点最小距离 action = new A_783_二叉搜索树节点最小距离();

	public static void main(String[] args) {
		test1();
		test2();
		test3();
	}


	public static void test1() {
		//

		//System.out.println("res = " + res);
	}

	public static void test2() {
		//

		//System.out.println("res = " + res);

	}

	public static void test3() {
		//

		//System.out.println("res = " + res);
	}

	public int minDiffInBST(TreeNode root) {
		LinkedList<Integer> list = new LinkedList<>();
		inorder(root, list);
		int min = 1000000;
		Iterator<Integer> iterator = list.iterator();
		int last = iterator.next();
		while (iterator.hasNext()) {
			int cur = iterator.next();
			min = Math.min(min, cur - last);
			last = cur;
		}
		return min;
	}

	void inorder(TreeNode root, List<Integer> list) {
		if (root == null) {
			return;
		}
		inorder(root.left, list);
		list.add(root.val);
		inorder(root.right, list);

	}
}
